import { GIcon, GModalButton, GModalForm, GTable } from 'geminis-react';
import { useRef, useState } from 'react';
import { message } from 'antd';
import { Package } from './package';

export default () => {
  const tableRef = useRef<any>();
  const [selection, setSelection] = useState({
    selectedKeys: [],
    selectedRows: [],
  });

  const schema = {
    headerTitle: '装箱',
    data: {
      url: '/api/warehouse/purchase/purchase',
      filter: { state: 'CHECK' },
      include: ['routes'],
      sort: 'checkTime:desc',
    },
    action: {
      row: {
        render: (record: any) => [
          <GModalForm
            button={{
              text: '完成装箱',
              button: {
                type: 'link',
              },
            }}
            form={{
              schema: {
                data: {
                  url: '/api/warehouse/purchase/purchase/pack',
                  methodSubmit: 'post',
                  format: 'form',
                },
              },
              schemaPath: '/warehouse-pc/schema/purchase/package/form.json',
              onSubmitting: (value) => {
                value.ids = [record.id];
              },
              onSubmitted: () => {
                message.success('成功');
                tableRef.current.loadData();
              },
            }}
          />,
        ],
      },
    },
    edit: {
      button: {
        text: '查看',
        modal: {
          width: '1000px',
        },
      },
      form: {
        schema: {
          data: {
            url: '/api/warehouse/purchase',
            includes: ['details'],
            methodSubmit: 'none',
          },
        },
        schemaPath: '/warehouse-pc/schemas/purchase/package/view.json',
      },
    },
  };

  return (
    <GTable
      ref={tableRef}
      onSelected={setSelection}
      schema={schema}
      schemaPath="/warehouse-pc/schema/purchase/package/table.json"
      external={{
        pos0: (
          <GModalButton
            text="装箱"
            button={{
              icon: <GIcon type="icon-start" />,
              disabled: selection?.selectedKeys.length !== 1,
            }}
            modal={{
              title: '货物装箱',
              width: '1000px',
            }}
            onOk={() => tableRef.current.loadData()}
          >
            <Package orderId={selection?.selectedKeys[0]} />
          </GModalButton>
        ),
      }}
    />
  );
};
